library(tidyverse)
library(haven)
library(labelled)
library(scales)
library(purrr)
library(tidyr)
library(broom)
rm(list=ls())


df<- read_rds("clean-GSS-institutional-trust.RDS")

## make it longer
df_long <- df %>%
  pivot_longer(consci:conlegis,names_to = "institution", values_to = "value")%>%
  #drop_na() %>% mutate(institution = as.character(recode(institution, 
  mutate(institution = as.character(recode(institution, 
                                           "consci" = "Scientific Community",
                                           "coneduc" = "Education",
                                           "conpress" = "Press",
                                           "contv"="Television",
                                           "conjudge" = "U.S. Supreme Court",
                                           "conmedic" = "Medicine",
                                           "conarmy" = "Military",
                                           "conclerg" = "Organized Religion",
                                           "conbus" = "Major Companies",
                                           "conlabor" = "Organized Labor",
                                           "confinan" = "Banks and Financial Institutions",
                                           "conlegis" = "Congress"))) %>%
  mutate(pid3 = as.character(dplyr::recode(partyid, 
                                           "0"="Democrat",
                                           "1" = "Democrat",
                                           "2" = "Democrat",
                                           "3" = "Independent",
                                           "4"="Republican",
                                           "5"="Republican",
                                           "6"="Republican",
                                           "7"="other")))%>%
  mutate(sex = as.character(dplyr::recode(sex, 
                                           `1` = "Male",
                                           `2` = "Female")))%>%
  mutate(race = as.character(dplyr::recode(race, 
                                           "1" = "White",
                                           "2" = "Black",
                                           "3" = "Other")))
  

means_party <- df_long %>%
  mutate(value = as.numeric(dplyr::recode(value, 
                                   "1" = "3",
                                   "2" = "2",
                                   "3" = "1"))) %>%
  subset(pid3 %in% c("Republican", "Democrat"))%>%
  group_by(year, institution, pid3) %>%
  summarise(mean_trust_i_per_year = weighted.mean(value, wtssps, na.rm=TRUE),
            sd = sd(value, na.rm=TRUE),
            se = sd / sqrt(n()),
            n_non_na = sum(!is.na(value)))

ggplot(means_party, aes(x=year, y=mean_trust_i_per_year, color=pid3)) + #geom_point() +
  geom_line(linetype="dashed") +
  theme_bw() +
  facet_wrap(vars(institution)) +
  geom_errorbar(aes(ymin = (mean_trust_i_per_year -1.96*se), 
                    ymax = (mean_trust_i_per_year + 1.96*se))) +
  scale_color_manual(values=c("grey","grey4"))+
  labs(x="", y="Category", color="Party",
       subtitle="As far as the people running these institutions are concerned, would you 
say you have a great deal of confidence, only some confidence, or hardly
any confidence at all in them? N=592,648 (GSS)
Mean and 95% Confidence Intervals") +
  theme(legend.position="bottom")+
  scale_y_continuous(limits = c(1,3), breaks=c(1,2,3),
                     labels = c("Hardly Any", "Only Some", "A Great Deal"))
ggsave("PNAS/3-by-party.png", width=12, height=8)





means_sex <- df_long %>%
  mutate(value = as.numeric(dplyr::recode(value, 
                                          "1" = "3",
                                          "2" = "2",
                                          "3" = "1"))) %>%
  group_by(year, institution, sex) %>%
  summarise(mean_trust_i_per_year = weighted.mean(value, wtssps, na.rm=TRUE),
            sd = sd(value, na.rm=TRUE),
            se = sd / sqrt(n()),
            n_non_na = sum(!is.na(value)))%>%
  drop_na()

ggplot(means_sex, aes(x=year, y=mean_trust_i_per_year, color=sex)) + #geom_point() +
  geom_line(linetype="dashed") +
  theme_bw() +
  facet_wrap(vars(institution)) +
  geom_errorbar(aes(ymin = (mean_trust_i_per_year -1.96*se), 
                    ymax = (mean_trust_i_per_year + 1.96*se))) +
  scale_color_manual(values=c("grey","grey4"))+
  labs(x="", y="Category", color="Sex",
       subtitle="As far as the people running these institutions are concerned, would you 
say you have a great deal of confidence, only some confidence, or hardly
any confidence at all in them? N=592,648 (GSS)
Mean and 95% Confidence Intervals") +
  theme(legend.position="bottom")+
  scale_y_continuous(limits = c(1,3), breaks=c(1,2,3),
                     labels = c("Hardly Any", "Only Some", "A Great Deal"))

ggsave("PNAS/3-by-sex.png", width=12, height=8)





means_race <- df_long %>%
  mutate(value = as.numeric(dplyr::recode(value, 
                                          "1" = "3",
                                          "2" = "2",
                                          "3" = "1"))) %>%
  subset(race %in% c("Black", "White"))%>%
  group_by(year, institution, race) %>%
  summarise(mean_trust_i_per_year = weighted.mean(value, wtssps, na.rm=TRUE),
            sd = sd(value, na.rm=TRUE),
            se = sd / sqrt(n()),
            n_non_na = sum(!is.na(value)))

ggplot(means_race, aes(x=year, y=mean_trust_i_per_year, color=race)) + #geom_point() +
  geom_line(linetype="dashed") +
  theme_bw() +
  facet_wrap(vars(institution)) +
  geom_errorbar(aes(ymin = (mean_trust_i_per_year -1.96*se), 
                    ymax = (mean_trust_i_per_year + 1.96*se))) +
  scale_color_manual(values=c("grey","grey4"))+
  labs(x="", y="Category", color="Race",
       subtitle="As far as the people running these institutions are concerned, would you 
say you have a great deal of confidence, only some confidence, or hardly
any confidence at all in them? N=592,648 (GSS)
Mean and 95% Confidence Intervals") +
  theme(legend.position="bottom")+
  scale_y_continuous(limits = c(1,3), breaks=c(1,2,3),
                     labels = c("Hardly Any", "Only Some", "A Great Deal"))
ggsave("PNAS/3-by-race.png", width=12, height=8)
